
<?php
     
class JsonController extends Zend_Controller_Action
{
    public function init()
    {
        $this->logger = Marjory_Service::getLog();
        parent::init();
    }

    /**
     * Exposes the Marjory_Service class as a JSON-RPC webservice.
     * Uses Marjory's native API instead of mimicking Solr.
     * Uses Dojo compatible output format.
     *
     * @todo Add possibility to limit exposed methods
     */
    public function indexAction()
    {
        $server = new Zend_Json_Server();
        // Indicate what functionality is available:
        $server->setClass('Marjory_Service');
        if ('GET' == $_SERVER['REQUEST_METHOD']) {
            $this->logger->info('SMD requested by ' . $_SERVER['REMOTE_ADDR']);
            // Indicate the URL endpoint, and the JSON-RPC version used:
            $server->setTarget('/json-rpc.php')
                    ->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
        
            // Grab the SMD
            $smd = $server->getServiceMap();
            // Set Dojo compatibility:
            $smd->setDojoCompatible(true);

            // Return the SMD to the client
            //header('Content-Type: application/json');
            echo $smd;
            return;
        }
        
        // Handle the request:
        $server->handle();
    }
}
